﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Core.ProcessManager.Client.UI;
using Sinacor.Infra.UI.Common;
using PSE.ProcessManager.Common.Process;

namespace Sinacor.UI.Bovespa.ImportacaoExportacao.Communication
{
    public class ProcessoImportacaoDistribuicao : BaseCommunicationProcess<Sinacor.Process.Bovespa.Distribuicao.Client.Importacao> 
    {
        #region Eventos

        public event EventHandler<ProcessEventArgs<Model.Parameter.FiltroRetornoImportacaoDistribuicao>> ImportacaoConcluida;
        public event Action ImportacaoIniciada;

        #endregion

        #region Construtor

        public ProcessoImportacaoDistribuicao(SessionInformation session)
            : base(session)
        {
            this.Process.ImportacaoConcluida += new EventHandler<ProcessEventArgs<Process.Bovespa.Distribuicao.ProcessEntity.Parameter.FiltroRetornoImportacaoDistribuicao>>(Process_ImportacaoConcluida);
            this.Process.ImportacaoIniciada += new EventHandler(Process_ImportacaoIniciada);
            Connect();
        }

        #endregion

        #region Event Handlers

        private void Process_ImportacaoIniciada(object sender, EventArgs e)
        {
            try
            {
                if (ImportacaoIniciada != null)
                    ImportacaoIniciada();
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
            }
        }

        private void Process_ImportacaoConcluida(object sender, ProcessEventArgs<Process.Bovespa.Distribuicao.ProcessEntity.Parameter.FiltroRetornoImportacaoDistribuicao> e)
        {
            Translator.ProcessEntityToModel.ImportacaoDistribuicao translator;
            try
            {
                if (ImportacaoConcluida != null)
                {
                    translator = new Translator.ProcessEntityToModel.ImportacaoDistribuicao();
                    ImportacaoConcluida(this, new ProcessEventArgs<Model.Parameter.FiltroRetornoImportacaoDistribuicao>(translator.ToFiltroRetornoImportacaoDistribuicao(e.Parameter)));
                }
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
            }
        }

        #endregion

        #region Métodos Públicos

        /// 
        /// <param name="model"></param>
        public void ImportarDistribuicao(Model.Parameter.FiltroImportacaoDistribuicao model)
        {
            Translator.ModelToProcessEntity.ImportacaoDistribuicao translatorRequest;

            try
            {
                translatorRequest = new Translator.ModelToProcessEntity.ImportacaoDistribuicao();
                this.Process.ImportarDistribuicao(translatorRequest.ToFiltroImportacaoDistribuicao(model));
            }
            catch (Exception ex)
            {
                base.HandleFault(ex);
            }
        }

        #endregion
    }
}
